#include"RRQueue.hpp"
#include"Task.hpp"

const string str = "+-*/%";

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* productor(void* args)
{
  RRQueue<Task>* rq = static_cast<RRQueue<Task>* >(args);
  while(true)
  {
     //sleep(1);
     int x = rand() % 100;
     int y = rand() % 100;
     char op = str[rand() % str.size()];
     Task task(x,y,op);
     rq->push(task);
     pthread_mutex_lock(&mutex);
     cout << "I am productor,I send task to RRQueue : " << task.FormatArg() << endl;
     pthread_mutex_unlock(&mutex);
  }
  return nullptr;
}

void* consumer(void* args)
{
  RRQueue<Task>* rq = static_cast<RRQueue<Task>* >(args);
  while(true)
  {
    //sleep(1);
    Task task;
    rq->pop(task);
    task();
    pthread_mutex_lock(&mutex);
    cout << "I am consumer,I receive task from RRQueue : " << task.FormatRes() << endl;
     pthread_mutex_unlock(&mutex);
  }
  return nullptr;
}

int main()
{
  RRQueue<Task>* rq = new RRQueue<Task>();
  srand(time(nullptr) ^ getpid());
  pthread_t c,p;
  pthread_create(&c,nullptr,consumer,rq); 
  pthread_create(&p,nullptr,productor,rq);
  
  pthread_join(c,nullptr);
  pthread_join(p,nullptr);
  return 0;
}
